package com.app.entity.authority;

import com.app.entity.authority.*;
import com.app.entity.authority.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * Created by lijun on 2018/2/11.
 */
public class SecurityUser extends User  implements UserDetails {
    private static final long serialVersionUID = 1L;

    public SecurityUser(User user){
        super.setName(user.getLoginname());
        super.setLoginname(user.getLoginname());
        super.setSRoles(user.getRoles());
        super.setDepartmentid(user.getDepartmentid());
        super.setEmail(user.getEmail());
        super.setEmpId(user.getEmpId());
        super.setMobile(user.getMobile());
        super.setPic(user.getPic());
        this.avatar=user.getPic();

        super.setCreatetime(user.getCreatetime());

        super.setPassword(user.getPassword());
        super.setCreatorName(user.getCreatorName());
        super.setModifierId(user.getModifierId());
        super.setModifytime(user.getModifytime());

      //  this.id=user.getId();

//        this.name=user.getName();
//        this.avatar=user.getPic();
//        this.email=user.getEmail();
//        this.mobile = user.getMobile();
//        this.loginname=user.getLoginname();
        super.setId(user.getId());
//        this.password=user.getPassword();
//        this.userRoles = user.getRoles();

        if(user.roles!=null && user.roles.size()>0){
            roles=new  String [user.roles.size()];
            int count=0;
            for (Role r:user.roles
                    ) {
                this.roles[count++] = r.getCode();
            }
        }




//        super.setPassword(user.getPassword());
//        super.setId(user.getId());


    }





    public String getLoginname() {
        return super.getLoginname();
    }

    public void setLoginname(String loginname) {
        super.setLoginname(loginname);
    }

 ///   public void setId(String id) {
   //     this.id = id;
//    }

    public void setName(String name) {
        this.name = name;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

//    public void setEmail(String email) {
//        this.email = email;
//    }
//
//    public void setMobile(String mobile) {
//        this.mobile = mobile;
//    }
//
//    public void setDepartmentid(String departmentid) {
//        this.departmentid = departmentid;
//    }
//
//    public void setPassword(String password) {
//        this.password = password;
//    }

  //  String loginname;


    @Override
    public  String getUsername(){
        return this.name;
    }




//    String id;
//    String name;
//
//    public String getId() {
//        return id;
//    }
//
//    public String getName() {
//        return name;
//    }
//
//    public String getIntroduction() {
//        return introduction;
//    }
//
//    public String getEmail() {
//        return email;
//    }
//
//    public String getMobile() {
//        return mobile;
//    }
//
//    public String getDepartmentid() {
//        return departmentid;
//    }

    String introduction;
//
    String avatar;

    public  String getAvatar(){
        return this.avatar;

    }

//
//    String email;//����
//
//
//    String mobile;//�绰
// String departmentid;





    public boolean isAccountNonExpired() {
        return true;
    }


    public boolean isAccountNonLocked() {
        return true;
    }


    public boolean isCredentialsNonExpired() {
        return true;
    }


    public boolean isEnabled() {
        return true;
    }
//String password;
    public String getPassword() {
        return password;
    }

      String[] roles;
    Set<Role> userRoles;

    public  String[] getRolesArray(){
        return  roles;
    }


    public Collection<? extends GrantedAuthority> getAuthorities() {

        Collection<GrantedAuthority> authorities = new ArrayList<>();


        if(userRoles != null)
        {
            for (Role role : userRoles) {
                //SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getName());
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_"+role.getCode().toUpperCase());
                authorities.add(authority);
            }
        }
//        else if(this.roles!=null){
//            for (String  r : roles) {
//                //SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getName());
//                SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_"+r.toUpperCase());
//                authorities.add(authority);
//            }
//        }
        return authorities;
    }


}
